﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;

namespace YJKRevitCode.ShortPathSuite
{
	public class ShortPathCalculateManger
	{
		public ShortPathCalculateManger(List<Curve> lstCurveCanGo, List<Curve> lstCurveCanNotGo, Dictionary<XYZ, object> inputStartPoints, Dictionary<XYZ, object> inputEndPoints, MidPointUsefulDel inputDel)
		{
			this.m_lstCanGoCurve = (from n in lstCurveCanGo
			where null != n
			select new BaseCurveBean(n, true, false)).ToList<BaseCurveBean>();
			this.n_lstCanNotGoCurve = (from n in lstCurveCanNotGo
			where null != n
			select new BaseCurveBean(n, false, true)).ToList<BaseCurveBean>();
			this.m_lstStartPoints = (from n in inputStartPoints
			where null != n.Key
			select new BaseEndPointPacker(n.Key, true, n.Value)).ToList<BaseEndPointPacker>();
			this.m_lstEndPoints = (from n in inputEndPoints
			where null != n.Key
			select new BaseEndPointPacker(n.Key, false, n.Value)).ToList<BaseEndPointPacker>();
			this.m_useCalculater = new BaseWapMapCalculater<BaseEndPointPacker>(this.m_lstCanGoCurve, this.n_lstCanNotGoCurve, inputDel);
		}

		public void Calculate()
		{
			foreach (BaseEndPointPacker startPoint in this.m_lstStartPoints)
			{
				this.m_useCalculater.SetStartPoint(startPoint);
				foreach (BaseEndPointPacker endPoint in this.m_lstEndPoints)
				{
					this.m_useCalculater.SetEndPoint(endPoint);
					ResultBean item;
					if (this.m_useCalculater.TryCalculate(out item))
					{
						this.m_lstAllResult.Add(item);
					}
				}
			}
		}

		public List<ResultBean> GetAllResult()
		{
			return this.m_lstAllResult;
		}

		public Dictionary<BaseEndPointPacker, List<ResultBean>> GetGroupedResult(bool ifByStartPoint)
		{
			IEnumerable<IGrouping<BaseEndPointPacker, ResultBean>> source;
			if (ifByStartPoint)
			{
				source = from k in this.m_lstAllResult
				group k by k.StartPoint;
			}
			else
			{
				source = from k in this.m_lstAllResult
				group k by k.EndPoint;
			}
			return source.ToDictionary((IGrouping<BaseEndPointPacker, ResultBean> k) => k.Key, (IGrouping<BaseEndPointPacker, ResultBean> k) => k.ToList<ResultBean>());
		}

		private BaseWapMapCalculater<BaseEndPointPacker> m_useCalculater;

		private List<BaseEndPointPacker> m_lstStartPoints = new List<BaseEndPointPacker>();

		private List<BaseEndPointPacker> m_lstEndPoints = new List<BaseEndPointPacker>();

		private List<BaseCurveBean> m_lstCanGoCurve = new List<BaseCurveBean>();

		private List<BaseCurveBean> n_lstCanNotGoCurve = new List<BaseCurveBean>();

		private List<ResultBean> m_lstAllResult = new List<ResultBean>();
	}
}
